package com.main.heath.servcie.impl;

import com.main.heath.mapper.PingdataExtendMapper;
import com.main.heath.po.PingList;
import com.main.heath.servcie.PingControlService;
import com.main.heath.servcie.util.PingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
 * Created by heathhose on 16-11-15.
 */
@Service
public class PingControlServiceImpl implements PingControlService {

    @Autowired
    private PingdataExtendMapper pingdataExtendMapper;

    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(6);
    PingUtil[] pingUtils = new PingUtil[6];
    int i = 0;


    public boolean startPing(String ipAddress) throws Exception {
        if(pingUtils[i] == null)
            pingUtils[i] = new PingUtil();
        pingUtils[i].setIpAddress(ipAddress);
        pingUtils[i].setFlag(true);
        fixedThreadPool.execute(pingUtils[i]);
        i++;
        if(i>5) i=0;
        return true;
    }

    public boolean endPing(String ip) throws Exception {

        for(int h = 0;h<6;h++){
            if(pingUtils[h].getIpAddress() == ip){
                pingUtils[h].setFlag(false);
                break;
            }
        }
        if(pingdataExtendMapper.deleteAllPingInfo()>0)
            return true;
        return false;
    }

    public boolean emptyPing() throws Exception {
        return false;
    }
}
